package com.jr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.jr.entity.Emp;
import com.jr.service.EmpService;
import com.jr.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

@Controller
/*@RestController *//*@RestController = @Controller + @ResponseBody*/
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private Gson gson;

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    public EmpService getEmpService() {
        return empService;
    }

/*    @RequestMapping("/show")
    @ResponseBody
    public String show(){
       return "我的第一个SpringBoot项目";
    }*/

    @RequestMapping("/login")
    public ModelAndView denglu(Emp emp){

        ModelAndView mv=new ModelAndView();
        mv.setViewName("show.html");
        return mv;
    }



    @RequestMapping("/getAll")
    @ResponseBody
    public Result getAll(Integer indexPage){
        if (indexPage==null){
            PageHelper.startPage(1,2);
        }else {
            PageHelper.startPage(indexPage,2);
        }
        PageInfo<Emp> info=new PageInfo<>(empService.findAll());
        return Result.success(info);
    }



    @RequestMapping("del")
    @ResponseBody
    public String del(Emp emp){
     empService.deleteEmp(emp.getEmpno());
     return gson.toJson(empService.findAll());
    }

    @RequestMapping("/select")
    @ResponseBody
    public Result selectByInfo(Emp emp,Integer indexPage){
        if (emp.getMgr()==0){
            emp.setMgr(null);
        } if (indexPage==null){
            PageHelper.startPage(1,2);
        }else {
            PageHelper.startPage(indexPage,2);
        }
        PageInfo<Emp> info=new PageInfo<>(empService.selectByInfo(emp));
        Result result=new Result();
        return result.success(info);
    }

    @RequestMapping("getAllMgr")
    @ResponseBody
    public String getAllMgr(){
        return gson.toJson(empService.selectMgr());
    }

    @RequestMapping("/index")
    public String goindex(){
        return "127.0.0.1:8080";
    }




}
